
package com.example.asa;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class EmergencyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 设置字符编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        // 验证用户角色权限
        HttpSession session = request.getSession();
        String role = (String) session.getAttribute("role");

        if (!"resident".equals(role)) {
            request.setAttribute("message", "权限不足，请先以 resident 身份登录");
            RequestDispatcher dispatcher = request.getRequestDispatcher("/login.jsp");
            dispatcher.forward(request, response);
            return;
        }

        // 数据库操作 - 查询灾情信息
        List<Disaster> disasterList = new ArrayList<>();

        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = MySQLDatabaseConnector.getConnection();
            if (conn == null) {
                System.err.println("Error: 数据库连接为空");
                request.setAttribute("message", "数据库连接失败");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/error.jsp");
                dispatcher.forward(request, response);
                return;
            }

            String sql = "SELECT * FROM disasters WHERE status = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, "预测"); // 查询状态为"预测"的灾情
            rs = stmt.executeQuery();

            while (rs.next()) {
                Disaster disaster = new Disaster();
                disaster.setId(rs.getInt("id"));
                disaster.setName(rs.getString("name"));
                disaster.setTimestamp(rs.getTimestamp("timestamp"));
                disaster.setLocation(rs.getString("location"));
                disaster.setAffected_area(rs.getString("affected_area"));
                disaster.setDescription(rs.getString("description"));
                disaster.setStatus(rs.getString("status"));
                disaster.setSeverity(rs.getString("severity"));
                disaster.setContact(rs.getString("contact"));

                disasterList.add(disaster);
            }

            request.setAttribute("disasterList", disasterList);
            RequestDispatcher dispatcher = request.getRequestDispatcher("/emergencyPage.jsp");
            dispatcher.forward(request, response);

        } catch (SQLException e) {
            System.err.println("Error fetching disasters: " + e.getMessage());
            e.printStackTrace();
            request.setAttribute("message", "查询灾情信息时发生错误");
            RequestDispatcher dispatcher = request.getRequestDispatcher("/error.jsp");
            dispatcher.forward(request, response);
        } finally {
            try {
                if (rs != null)
                    rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
